$(document).ready(function () {
  $('#username').val('');
  $('#password').val('');

  var isLogin = localStorage.getItem('sessionId');
  if (isLogin != null && isLogin != '') {
    window.location.href = 'index.html';
  }
  initSize();
});

/*初始化默认字体大小*/
function initSize() {
  //适应不同分辨率
  let fontSize;
  resize();
  window.onresize = resize;

  function resize() {
    let winW = window.innerWidth;

    if (winW < 1366) {
      winW = 1366;
    }
    fontSize = ((winW / 1920) * 100 * 1366) / 1920;
    document.getElementsByTagName('html')[0].style.cssText =
      'font-size:' + fontSize + 'px';
  }

  // let userInfo = JSON.parse(localStorage.getItem("isLogin"));
  // $('#userName').html(userInfo.name);
}

//登录验证
$('.btn-info').click(function () {
  if ($('#username').val() == '') {
    $('#login-msg').find('label').eq(0).html('请输入用户名');
  } else if ($('#password').val() == '') {
    $('#login-msg').find('label').eq(0).html('请输入密码');
  } else {
    login();
  }
});
//回车确认事件
document.onkeydown = function (event) {
  if (event.keyCode == 13) {
    $('.btn-info').trigger('click');
  }
};

function login() {
  var account = $('#username').val();
  var password = $('#password').val();

  var url = '/rest/jxsh/login';
  $.ajax({
    url: url,
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({
      account: account,
      password: password,
    }),
    success: function (data) {
      var status = data.status;
      var data = data.result;
      if (status == 200) {
        localStorage.setItem('orgId', data.orgId);
        localStorage.setItem('userInfo', JSON.stringify(data));
        localStorage.setItem('sessionId', JSON.stringify(data.sessionId));
        window.location.href = '../html/index.html';
      } else {
        $('#login-msg').find('label').eq(0).html('用户名或密码错误');
      }
    },
    error: function (result) {},
  });
}
